package il.ac.braude.sona.gui.infogui;

import il.ac.braude.sona.gui.SONAFrame;

import java.awt.Color;
import java.awt.Font;
import java.util.HashMap;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.MultiplePiePlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.TableOrder;

/**
 * Class for creating the graph for the delays report, uses the jfreechat jar to create the dataset and graph
 *
 * @author  Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 * Laboratory in Software Engineering,Ort Braude College.</p>
 *
 */
@SuppressWarnings("serial")
public class DelaysGraphFrame extends SONAFrame {

	/**
	 * Constructor for the graph
	 *
	 * @param reportAnswer - HashMap contains all information for the report
	 * @param reportName - the report name
	 */
	public DelaysGraphFrame(HashMap<String, String> reportAnswer, String reportName)
	{
		super();
		final CategoryDataset dataset = createDataset(reportAnswer,  reportName);
		// create the chart...
		final JFreeChart chart = createChart(dataset,reportName);

		final ChartPanel chartPanel = new ChartPanel(chart,true,true,true,false,true);
		/**
		 * adds the created graph panel to the frame
		 */
		this.add(chartPanel);
	}

	/**
	 * Creates the dataset for the graph.using the fields and values of the report
	 * @param reportAnswer - HashMap contains all information for report
	 * @param reportName - the report name
	 *
	 * @return dataset for the graph to be build on.
	 */
	private CategoryDataset createDataset(HashMap<String, String> reportAnswer, String reportName) {

		final String system[] = {"Braude web-site","Moodle","Student info. station","Worker info. station","Lecturer info. station",
				"Class computers","Labs & guests PCs","Library System" };
		final String sum = "Number of delayed days ";
		final String num = "Number of stage delays ";
		final String stDevDays = "Standard devaition for delayed days ";
		final String stDevDelays = "Standard devaition for delayes ";

		final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		for (int i = 0; i<system.length;i++)
		{
			if (reportAnswer.containsKey(sum+system[i]))
				dataset.addValue(Integer.parseInt(reportAnswer.get(sum+system[i])), sum, system[i]);
			if (reportAnswer.containsKey(num+system[i]))
				dataset.addValue(Integer.parseInt(reportAnswer.get(num+system[i])), num, system[i]);
			if (reportAnswer.containsKey(sum+system[i])) {
				int times = Integer.parseInt(reportAnswer.get(sum+system[i]));
				int std = Integer.parseInt(reportAnswer.get("Median for all"));
				dataset.addValue((Math.abs(times -std)),stDevDays, system[i]);         
			}
		}        
		return dataset;
	}
	/**
	 * creates the multiple pie chart using chart factory class from the jar file
	 * @param dataset - the dataet to be used
	 * @param reportName - the report name
	 * @return the created chart
	 */
	private JFreeChart createChart(final CategoryDataset dataset, String reportName) {
		final JFreeChart chart = ChartFactory.createMultiplePieChart(
				reportName, dataset, TableOrder.BY_COLUMN, false, true, false
				); 

		chart.setBackgroundPaint(new Color(216, 255, 216));
		final MultiplePiePlot plot = (MultiplePiePlot) chart.getPlot();
		final PiePlot p = (PiePlot) plot.getPieChart().getPlot();
		p.setMaximumLabelWidth(0.35);
		p.setLabelFont(new Font("SansSerif", Font.ITALIC, 8));
		p.setInteriorGap(0.20);
		return chart;
	}
}


